﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlackBird.Models;
using BlackBird.ControllerHelpers;
using System.Web.Script.Serialization;

namespace BlackBird.DataAccess
{
    public static class FuKuanDanDataAccess
    {
        public static List<FuKuanDanModel> AllFuKuanDanDM()
        {
            using (var dbContext = new blackbirdEntities())
            {
                var fukuandanList = dbContext.fukuandan.ToList();
                var dmListForCurrentProject = fukuandanList.Select<fukuandan, FuKuanDanModel>(p => BuildFuKuanDanDM(p));

                return dmListForCurrentProject.ToList();
            }
        }

        public static List<FuKuanDanModel> AllFuKuanDanDMForCurrentProject(int projectId)
        {
            using (var dbContext = new blackbirdEntities())
            {
                var fukuandanList = dbContext.fukuandan.ToList();
                var funkuandanListForCurrentProject = fukuandanList.FindAll(p => p.Project_idProject == projectId);

                var dmListForCurrentProject = funkuandanListForCurrentProject.Select<fukuandan, FuKuanDanModel>(p => BuildFuKuanDanDM(p));

                return dmListForCurrentProject.ToList();
            }
        }

        public static fukuandan FindFuKuanDanDbObject(int id)
        {
            using (var dbContext = new blackbirdEntities())
            {
                var fukuandanList = dbContext.fukuandan.ToList();
                var findFuKuanDan = fukuandanList.Find(p => p.idFuKuanDan == id);
                return findFuKuanDan;
            }
        }

        public static FuKuanDanModel FindFuKuanDan(int id)
        {
            using (var dbContext = new blackbirdEntities())
            {
                var fukuandanList = dbContext.fukuandan.ToList();
                var findFuKuanDan = fukuandanList.Find(p => p.idFuKuanDan == id);
                if (findFuKuanDan == null) return null;

                return BuildFuKuanDanDM(findFuKuanDan);
            }
        }


        public static void AddFuKuanDan(FuKuanDanModel dm, Controller ctrl)
        {
            using (var dbContext = new blackbirdEntities())
            {
                var fukuandanDb = new fukuandan();

                fukuandanDb.CurrentStep = dm.CurrentStep;
                fukuandanDb.Project_idProject = Convert.ToInt32(dm.ProjectId);

                fukuandanDb.User_idUser = dm.CreateUser_idUser;

                var currentStatus = dm.Status.ToString();
                fukuandanDb.Status_idStatus = CommonDataAccess.GetSupportTypeStatusId(dm.Status);

                //save Fukuandan DM object as json
                var fukuandan_json_str = new JavaScriptSerializer().Serialize(dm);
                fukuandanDb.TableContent = fukuandan_json_str;

                dbContext.fukuandan.AddObject(fukuandanDb);
                dbContext.SaveChanges();

                dm.Id = fukuandanDb.idFuKuanDan;
            }

            ProjectDataAccess.AddFuKuanDanToProject(dm.ProjectId);
        }

        public static void UpdateFuKuanDan(FuKuanDanModel dm, Controller ctrl)
        {
            var originalDbObject = FindFuKuanDanDbObject(dm.Id);
            if (originalDbObject == null)
            {
                AddFuKuanDan(dm, ctrl);
                return;
            }

             using (var dbContext = new blackbirdEntities())
            {
                var fukuandanList = dbContext.fukuandan.ToList();
                var findFuKuanDan = fukuandanList.Find(p => p.idFuKuanDan == dm.Id);

                findFuKuanDan.CurrentStep = dm.CurrentStep;

                var currentStatus = dm.Status.ToString();
                findFuKuanDan.Status_idStatus = CommonDataAccess.GetSupportTypeStatusId(dm.Status);

                //save Fukuandan DM object as json
                var fukuandan_json_str = new JavaScriptSerializer().Serialize(dm);
                findFuKuanDan.TableContent = fukuandan_json_str;

                dbContext.SaveChanges();
             }
        }

        public static FuKuanDanModel BuildFuKuanDanDM(fukuandan dbObj)
        {
            if (dbObj == null) return null;

            FuKuanDanModel dmObj = null;

            //deserialize Fukuandan DM object from json
            if (dbObj.TableContent != null)
                dmObj = new JavaScriptSerializer().Deserialize<FuKuanDanModel>(dbObj.TableContent);
            if (dmObj == null)
                dmObj = new FuKuanDanModel();

            dmObj.Id = dbObj.idFuKuanDan;
            dmObj.CurrentStep = (int)dbObj.CurrentStep;
            dmObj.ProjectId = dbObj.Project_idProject.ToString();
            dmObj.CreateUser_idUser = dbObj.User_idUser;
            dmObj.Status = CommonDataAccess.GetSupportTypeStatusById((int)dbObj.status.idStatus);

            return dmObj;
        }
    }
}